<template>
  <div class="dashboard-container">
    <div class="app-container">
      <pageto :show-before="true">
        <span slot="before">共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push({path:'/import'})">导入</el-button>
          <el-button size="small" type="danger" @click="expors">导出</el-button>
          <el-button size="small" type="primary" @click="show=true">新增员工</el-button>
        </template>

      </pageto>

      <!-- 放置表格和分页 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogshow"
        width="30%"
        :before-close="handleClose"
      >
        <canvas ref="canvas" />
      </el-dialog>

      <el-card>
        <addcom :show-dialog="show" @close="show=false" @ok="getEmployeeList" />
        <el-table v-loading="loading" border :data="list">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="头像" sortable="">
            <template #default="{row}">
              <img v-imgerr="'https://img2.baidu.com/it/u=2421505363,3507499484&fm=26&fmt=auto&gp=0.jpg'" :src="row.staffPhoto" class="img" @click="dialogshows(row.staffPhoto)">
            </template>
          </el-table-column>
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formatters" />
          <el-table-column label="部门" sortable="" prop="departmentName" :formatter="departmentName" />
          <el-table-column label="入职时间" sortable="">
            <template #default="{row}">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="">
            <template #default="{row}">
              <!-- v-model是:value和@input事件的结合，:value是显示页面的数据 @input是控制value的组合就是双向绑定-->
              <el-switch
                :value="row.enableState===1?true:false"
                active-color="#13ce66"
                inactive-color="#ff4949"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/editpro/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="Rolists(row.id)">角色</el-button>
              <el-button type="text" size="small" @click="deletst(row.id)">辞退</el-button>
            </template>
          </el-table-column>
        </el-table>

        <roledialog ref="addrole" :roleshow.sync="Rolist" />
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :total="page.total"
            @current-change="changePage"
          />
        </el-row>
      </el-card>
    </div>
  </div>
</template>
<script>
import Qrcode from 'qrcode'
import roledialog from './components/addrole.vue'
import a from '@/api/constant/employees'
import { getEmployeeList, delets } from '@/api/employees'
import { formatDate } from '@/filters'
import addcom from './components/add.vue'

// import {export_table_to_excel} from '@/vendor/Export2Excel'
export default {
  components: {
    addcom, roledialog
  },
  // filters: {
  //   timefilter(cellvwlue) {
  //     const b = new Date(cellvwlue)
  //     const y = b.getFullYear()
  //     const m = b.getMonth() + 1
  //     const d = b.getDate()
  //     return `${y}年${m}月${d}日`
  //   }

  // },
  data() {
    return {
      Rolist: false,
      show: false,
      loading: false,
      dialogshow: false,
      list: [], // 接数据的
      page: {
        page: 1, // 当前页码
        size: 10,
        total: 0 // 总数
      }

    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    Rolists(id) {
      console.log(id)
      this.$refs.addrole.getRoleDetail(id)
      this.Rolist = true
      console.log(21)
    },
    handleClose() {
      this.dialogshow = false
    },
    dialogshows(a) {
      if (a) {
        this.dialogshow = true
        this.$nextTick(() => {
          Qrcode.toCanvas(this.$refs.canvas, a)
        })
      }
    },
    async expors() {
      // 按需加载
      const { export_json_to_excel } = await import('@/vendor/Export2Excel')
      const dalist = (await getEmployeeList({ page: 1, size: 5 })).rows
      console.log(dalist)
      const dict = {
        '手机号': 'mobile',
        '姓名': 'username',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      const header = Object.keys(dict)
      const data = dalist.map(v => {
        return this.obj2arr(v, dict)
      })
      console.log(data, header)

      await export_json_to_excel({ header: header, data: data })
      // console.log(export_json_to_excel)
    },
    departmentName(a, b, c) {
      return c || '沙雕部'
    },

    formatters(row, col, cellvwlue) {
      const b = a.hireType.find(v => v.id === +cellvwlue)
      return b ? b.value : '未知'
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getEmployeeList()
    },
    async getEmployeeList() {
      this.loading = true
      const { total, rows } = await getEmployeeList(this.page)
      this.page.total = total
      this.list = rows
      this.loading = false
    },
    // 数据转换
    obj2arr(v, dict) {
      const c = []
      for (const key in dict) {
        const a = dict[key]
        let b = v[a]
        if (a === 'timeOfEntry' || a === 'correctionTime') {
          b = formatDate(v[a])
        }
        if (a === 'formOfEmployment') {
          b = this.formatters(v[a])
        }
        c.push(b)
      }
      return c
    },
    // 删除
    async deletst(id) {
      try {
        await this.$confirm('你确定要辞退该员工？')
        await delets(id)
        // 提示
        this.$message.success('已辞退')
        if (this.page.page !== 1 && this.list.length === 1) {
          console.log(12345568894455)
          this.page.page--
        } else {
          console.log(2644561)
        }
        // 刷新页面
        this.getEmployeeList()
      } catch {
        console.log(new Error())
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.img{
  width: 90%;
  height: 130px;
  object-fit: cover;
}
</style>
